<template> 
    <el-card class="form-container" shadow="never">
        <el-form :model="shopExpense" :rules="rules" ref="ShopExpenseFrom" label-width="150px">


                    <!--
                        <el-form-item
                            label="类别编号"
                            prop="id">
                        <el-input v-model="shopExpense.id" style="width: 370px;"/>
                    </el-form-item>

                     -->

                     <el-form-item
                     label="类别"
                            prop="cateId">
                    <el-select @change="cateChange" v-model="shopExpense.cateId">
                        <el-option
                        v-for="item in cates"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                        </el-option>
                    </el-select>
                    </el-form-item>

                    <!--
                         <el-form-item
                            label="分类编号"
                            prop="cateId">
                        <el-input v-model="shopExpense.cateId" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="类名名称"
                            prop="cateName">
                        <el-input v-model="shopExpense.cateName" style="width: 370px;"/>
                    </el-form-item>
                     -->


                    <el-form-item
                            label="支出项目"
                            prop="items">
                        <el-input v-model="shopExpense.items" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="支出人"
                            prop="username">
                        <el-input v-model="shopExpense.username" style="width: 370px;"/>
                    </el-form-item>
                    <el-form-item
                            label="支出金额"
                            prop="items">
                             <el-input-number  :precision="2" v-model="shopExpense.money" ></el-input-number>
                    </el-form-item>

                   <!--
                        <el-form-item
                            label="创建人编号"
                            prop="createBy">
                        <el-input v-model="shopExpense.createBy" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="创建人姓名"
                            prop="createName">
                        <el-input v-model="shopExpense.createName" style="width: 370px;"/>
                    </el-form-item>
                    -->


                    <el-form-item
                            label="支出时间"
                            prop="createTime">
                            <el-date-picker
                                v-model="shopExpense.createTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择日期">
                                </el-date-picker>
                    </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="onSubmit('ShopExpenseFrom')">提交</el-button>
                <el-button v-if="!isEdit" @click="resetForm('ShopExpenseFrom')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
    import {createShopExpense, getShopExpense, updateShopExpense} from '@/api/mer/shopExpense'
    
    import {fetchList} from '@/api/mer/shopExpenseCategory'
    import SingleUpload from '@/components/Upload/singleUpload'

    const defaultShopExpense = {
        name: ''
    };
    export default {
        name: 'ShopExpenseDetail',
        components: {SingleUpload},
        props: {
            isEdit: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
            cates:[],
            shopExpense:
            Object.assign({},
        defaultShopExpense),
            rules: {
                name: [
                    {required: true, message: '请输入品牌名称', trigger: 'blur'},
                    {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
                ],
                    logo
            :
                [
                    {required: true, message: '请输入品牌logo', trigger: 'blur'}
                ],
                    sort
            :
                [
                    {type: 'number', message: '排序必须为数字'}
                ],
            }
        }
        },
        created() {
            if (this.isEdit) {
                getShopExpense(this.$route.query.id).then(response => {
                    this.shopExpense = response.data;
            })
                ;
            } else {
                this.shopExpense = Object.assign({},
            defaultShopExpense)
                ;
            }
            this.getCates();
        },
        methods: {
            cateChange(v){
                for(let item of this.cates){
                    if(item.id==v){
                        this.shopExpense.cateName = item.name;
                        break;
                    }
                }
                    
            },
            getCates(){this.cates
                fetchList({
                    pageNum: 1,
                    pageSize: 1000
                }).then(response => {
                    if(response.data.records){
                        for(let item of response.data.records){
                            this.cates.push({
                                id:item.id,
                                name:item.name
                            })
                        }
                    }
                    })
            },
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if(valid) {
                        this.$confirm('是否提交数据', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            if(this.isEdit
                    )
                        {
                            updateShopExpense(this.$route.query.id, this.shopExpense).then(response => {
                                if(response.code == 200
                        )
                            {
                                this.$refs[formName].resetFields();
                                this.$message({
                                    message: '修改成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    else
                        {
                            createShopExpense(this.shopExpense).then(response => {
                                if(response.code == 200
                        )
                            {
                                this.$refs[formName].resetFields();
                                this.shopExpense = Object.assign({},
                            defaultShopExpense)
                                ;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    })
                        ;

                    } else {
                        this.$message({
                            message: '验证失败',
                            type: 'error',
                            duration: 1000
                        });
                return false;
            }
            })
                ;
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.shopExpense = Object.assign({},
            defaultShopExpense)
                ;
            }
        }
    }
</script>
<style>
</style>


